/*
 * threadpool.cpp
 *
 *  Created on: 2012-5-2
 *      Author: epmm
 */
#include <signal.h>
#include "log.h"
#include "head.h"
#include "macro.h"
#include "jobbase.h"
#include "threadmutex.h"
#include "threadcondition.h"
#include "worktask.h"
#include "workthread.h"
#include "threadpool.h"

CThreadPool::CThreadPool()
:m_nWorkThreadNum(0),m_WorkTask(true,0)
{



}

CThreadPool::CThreadPool(int threadnum,bool block,int max_queue)
:m_nWorkThreadNum(threadnum),m_WorkTask(block,max_queue)
{

}
CThreadPool::~CThreadPool()
{
	// TODO Auto-generated destructor stub
}

int CThreadPool::AddWorkJob(CJobBase* pJob)
{
	m_WorkTask.addJob(pJob);
	return 0;
}
CJobBase* CThreadPool::GetWorkJob()
{
	return m_WorkTask.getJob();
}

void CThreadPool::CreateWorkThread(int nNum)
{
	LOG_PRINT_TRACE("CreateWorkThreadSync begin");
	LOG_PRINT_TRACE("Thread Num : "<<nNum);

	pthread_t thread_id;
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

	//signal(SIGPIPE, SIG_IGN);
 	for (int i = 0; i < nNum; i++)
	{
		pthread_create(&thread_id, NULL, CWorkThread::startThreadMain, this);
	}

	pthread_attr_destroy(&attr);

	LOG_PRINT_TRACE("CreateWorkThreadSync end");
}
void CThreadPool::InitPool(int threadnum, bool block, int max_queue)
{
	m_WorkTask.SetBlockMode(block);
	m_WorkTask.SetMaxTaskSize(max_queue);
 	CreateWorkThread(threadnum);
}
void CThreadPool::InitPool()
{
 	CreateWorkThread(m_nWorkThreadNum);
}

